package su.samcom.calc.model.dao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import su.samcom.calc.model.entity.Location;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Владислав
 * Date: 24.11.13
 * Time: 12:41
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class LocationDaoImpl extends BaseDaoImpl<Location> implements LocationDao {
    @Override
    protected Class<Location> getGeneric() {
        return Location.class;
    }


    @Override
    public List<Location> getLikeLocations(String name) {
        String sql = "from Location l where l.name like :name order by l.name";
        Query query = sessionFactory.getCurrentSession().createQuery(sql)
                .setParameter("name", String.format("%s%%", name));
        List<Location> locations = query.list();

        query = sessionFactory.getCurrentSession().createQuery(sql)
                .setParameter("name", String.format("_%%s%%", name));
        locations.addAll(query.list());

        return locations;
    }

    @Override
    public List<Location> getLikeLocations(String name, String countryName) {
        String sql = "from Location l where lower(l.name) like lower(:name) and lower(l.country.name) like lower(:countryName) order by l.name, l.country.name";
        Query query = sessionFactory.getCurrentSession().createQuery(sql)
                .setParameter("name", String.format("%s%%", name))
                .setParameter("countryName", String.format("%s%%", countryName));
        List<Location> locations = query.list();

        query = sessionFactory.getCurrentSession().createQuery(sql)
                .setParameter("name", String.format("_%%%s%%", name))
                .setParameter("countryName", countryName);
        locations.addAll(query.list());

        return locations;
    }

    @Override
    public List<Location> getLikeLocations(String name, Long countryId) {
        String sql = "from Location l where lower(l.name) like lower(:name) and l.country.id = :countryId order by l.name";
        Query query = sessionFactory.getCurrentSession().createQuery(sql)
                .setParameter("name", String.format("%s%%", name))
                .setParameter("countryId", countryId);
        List<Location> locations = query.list();

        query = sessionFactory.getCurrentSession().createQuery(sql)
                .setParameter("name", String.format("_%%%s%%", name))
                .setParameter("countryId", countryId);
        locations.addAll(query.list());

        return locations;
    }

    @Override
    public List<Location>  getLocations(String name, Long countryId) {
        String sql = "from Location l where lower(l.name) = lower(:name) and l.country.id = :countryId";
        Query query = sessionFactory.getCurrentSession().createQuery(sql)
                .setParameter("name", name)
                .setParameter("countryId", countryId);
        List<Location> locations = query.list();
        return locations;
    }
}
